package lib_model.Product;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import lib_dal.Connection.ConnectMSSQL;
import lib_dal.Interface.ISql;
import lib_model.Orther.ReturnObj;

public class Image_products implements ISql {

	public int id;
	public int product_id_ref;
	public String image_url;
	public String description;
	public int type;
	public Date date_create;

	public Image_products() {
	}

	public Image_products(int product_id_ref) {
		super();
		this.product_id_ref = product_id_ref;
	}

	@Override
	public ReturnObj Select() {
		String sql = "select id, product_id_ref, image_url, description, type, date_create from dbo.Image_products";
		ReturnObj obj = new ReturnObj();
		ArrayList<Image_products> arr = new ArrayList<Image_products>();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			ResultSet rs = preState.executeQuery();
			while (rs.next()) {
				Image_products a = new Image_products();
				a.id = rs.getInt("id");
				a.product_id_ref = rs.getInt("product_id_ref");
				a.image_url = rs.getString("image_url");
				a.description = rs.getString("description");
				a.type = rs.getInt("type");
				a.date_create = rs.getDate("date_create");
				arr.add(a);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		obj.ObjResult = arr;
		return obj;
	}

	@Override
	public ReturnObj Add() {
		String sql = "insert into dbo.Image_products (product_id_ref,   image_url,   description,   type,   date_create) values   ( ?,   ?,   ?,   ?,   GETDATE())";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.product_id_ref);
			preState.setString(2, this.image_url);
			preState.setString(3, this.description);
			preState.setInt(4, this.type);
			obj.Result = preState.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj Update() {
		String sql = "update dbo.Image_products set product_id_ref = ?,   image_url = ?,   description = ?,   type = ?,   date_create = ? where  id = ?";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.product_id_ref);
			preState.setString(2, this.image_url);
			preState.setString(3, this.description);
			preState.setInt(4, this.type);
			preState.setDate(5, (java.sql.Date) this.date_create);
			preState.setInt(6, this.id);
			obj.Result = (preState.executeUpdate() != 0) ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj Delete() {
		String sql = "delete from dbo.Image_products where id = ?";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.id);
			obj.Result = preState.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj GetInformation() {
		ReturnObj obj = new ReturnObj();
		String sql = "select id, product_id_ref, image_url, description, type, date_create from dbo.Image_products where id = ?";
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.id);
			ResultSet rs = preState.executeQuery();
			while (rs.next()) {
				Image_products a = new Image_products();
				a.id = rs.getInt("id");
				a.product_id_ref = rs.getInt("product_id_ref");
				a.image_url = rs.getString("image_url");
				a.description = rs.getString("description");
				a.type = rs.getInt("type");
				a.date_create = rs.getDate("date_create");
				obj.ObjResult = a;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	public ReturnObj Select(int product_id_ref) {
		String sql = "select * from dbo.Image_products where product_id_ref = ?";
		ReturnObj obj = new ReturnObj();
		ArrayList<Image_products> arr = new ArrayList<Image_products>();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, product_id_ref);
			ResultSet rs = preState.executeQuery();
			while (rs.next()) {
				Image_products a = new Image_products();
				a.id = rs.getInt("id");
				a.product_id_ref = rs.getInt("product_id_ref");
				a.image_url = rs.getString("image_url");
				a.description = rs.getString("description");
				a.type = rs.getInt("type");
				a.date_create = rs.getDate("date_create");
				arr.add(a);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		obj.ObjResult = arr;
		return obj;
	}

}
